package og.example.views.student;
import og.example.entity.Student;
import og.example.service.StudentService;
import og.example.views.MainFrame;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.List;

public class StudentListFrame extends JFrame {
    private JTable studentTable;

    public StudentListFrame() {
        setTitle("学生列表");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        String[] columnNames = {"ID", "学号", "姓名", "小组名称"};
        DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);

        List<Student> students = new StudentService().studentQuery();
        for (Student student : students) {
            Object[] row = {student.getId(), student.getStudentId(), student.getStudentName(), student.getGroupName()};
            tableModel.addRow(row);
        }

        studentTable = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(studentTable);

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(scrollPane, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel();
        JButton backButton = new JButton("返回主界面");

        buttonPanel.add(backButton);

        backButton.addActionListener(e -> {
            dispose(); // 关闭当前窗口
            new MainFrame(); // 打开主界面
        });

        getContentPane().add(buttonPanel, BorderLayout.SOUTH);

        setVisible(true);
    }
}
